package yf.common;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;


import java.util.Map;

import static io.restassured.RestAssured.given;

public class RunBaseCase {

    /*
     *post或get方式请求,返回响应报文（json格式）
     *@bodyString:json格式的请求报文体
     *@para:requestType post或get
     */

    public static Response runCase(String bodyString, String requestType, String path){
        Response response = null;
        Map<String, Object> map=null;
        if(requestType.toLowerCase().equals("post")==false){
            JSONObject jsonObject = JSONObject.parseObject(bodyString);
            map = (Map) jsonObject;
        }
        switch (requestType.toLowerCase()){
            case "get":
                response = given()
                        .contentType("application/json;charset=UTF-8")
                        .request()
                        .params(map)
                        .get(path);
                break;
            case "post":
                response = given()
                        .contentType("application/json;charset=UTF-8")
                        .request()
                        .body(bodyString)
                        .post(path);
                break;
        }
//        if(requestType.toLowerCase().equals("get")) {
//             JSONObject jsonObject = JSONObject.parseObject(bodyString);
//             Map<String, Object> map = (Map) jsonObject;
////             new Gson().toJson(map);
//            response = given()
//                    .contentType("application/json;charset=UTF-8")
//                    .request()
//                    .params(map)
//                    .get(path);
//
//        }
//        else {
//            response = given()
//                    .contentType("application/json;charset=UTF-8")
//                    .request()
//                    .body(bodyString)
//                    .post(path);
//
//        }
        return response;
    }


}
